<?php
class main extends spController
{

	//主检测模块http检测
	function __construct(){ 
		parent::__construct(); 
	}
	function index(){ // 这里是首页
		$this->contents = "产品模块首页内容";
		$runtime=spClass('runtime');
		$this->list=$runtime->findAll();
		$this->display("produce/main_index.html");
	}
	
	function page(){ // 其他内容
		echo '<h2>'.'检测功能'.$i.'</h2>';
		
		$this->start_detection();
		$this->contents = "这里是检测木块";
		$this->display("produce/main_page.html");
	}
	function start_detection()
	{
		//这里是开启检测功能
		//$this->detection();
		$time = $this->spArgs('time');
		$this->moniter($time);
	}

	function stop_detection()
	{
		//这里是停止检测功能
	}

	//检测
	public function detection($interval)
	{
		$site = spClass("site");
		$where['active'] = 1;
		$tttc=60;
		$where['intervals'] = $interval / $tttc;
		$time_id = $where['intervals'];
		$b = $site->findAll($where);
		dump($b);
		foreach ($b as $i)
		{
			$runtime = spClass("runtime");
			$runtime->u_time($time_id);
			
			$url = 'http://'.$i[domain];
			$s_date = date("Y-m-d H:i:s");
			$status=$this->http_p($url);
			$n_date = date("Y-m-d H:i:s");
			$sb=strtotime($n_date)-strtotime($s_date);

			import("detection.php");
			$get_ip = new detection();
			$ip=$get_ip->g_ip($i[domain]);

			$Model = spClass('record');
			$data = array
			(
				'status_code'=>$status,
				'userid'=>$i[userid],
				'siteid'=>$i[siteid],
				'type'=>'',
				'size'=>'0',
				'taketime'=>$sb,
				'addtime'=>strtotime(date("Y-m-d H:i:s")),
				'site_ip'=>$ip,				
			);
			$Model->create($data);
			
			$user = spClass('user');
			$conditions = array
			(
				'userid'=> $i[userid]
			);
			$u = $user->findall($conditions);
			$Model->alarm($i[siteid], $i[domain], $u[username], $u[mail], $i[userid]);//告警
			
			$ssdate= array
			(
				'status_code' => $status,
				'lasttime' => $data['addtime'],
			);
			$wwe['siteid']=$i[siteid];
			$site->update($wwe,$ssdate);
		}
		 
	}

	/**
	 +----------------------------------------------------------
	 * 网站链接测试
	 +----------------------------------------------------------
	 */
	public function http_p($url,$method,$postDate)
	{
		$method ='GET';
		$postDate = '';
		import("WebRequest.php");
		$ht_tp = new WebRequest($url,$method,$postDate);

		if (!$c=$ht_tp->getResponse())
		{
			return 'could not open';
		}
		else
		{
			return $c->status;
		}
	}

	/**
	 +----------------------------------------------------------
	 * 循环
	 +----------------------------------------------------------
	 */

	private function moniter($time)
	{
		ignore_user_abort(); 
		set_time_limit(0); 
		$interval= $time;
		do
		{
			$this->detection($interval);
			sleep($interval);
		}while(true);
	}

	function alarm($siteid)
	{
		$record = spClass('record');
		$n = $record->etc($siteid);
		if($n[0][status_code]=200)
		{
			if($n[1][status_code]=200)
			{
				break;
			}else
			{
				$time = $n[0][addtime]-$n[1][addtime];
			}
		}
		else
		{
			if ($n[1][status_code]=200)
			{
				$time = $n[0][addtime]-$n[1][addtime];
			}
			else
			{
				break;
			}
		}
	}

}